<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 矿机管理
 */
namespace app\api\model;

use app\api\model\Model;
use Mscore\Core\Redis;

class Trade extends Model{
    
    private $product = 'pro_trade';
    private $category = 'pro_category';
    private $order = 'pro_tradeorder';
    private $orderlog = 'pro_tradeorderlog';
    private $hashfix;
    private $config;

    /**
     * 初始化自动连接方法
     * @param array $__construct 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->config=config('futures');
        $this->hashfix=config('redis.redis_db.db_hashfix');
    }

    /**
     * 产品类别列表
     * @param array $catelist 
     * @param array $where 
     */ 
    public function catelist($where=null, $page=1, $page_size=10, $field='*'){
        //矿机列表
        $list['data']= Model::name($this->category)
            ->alias('a')
            ->field($field)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('a.orderid desc, a.id desc')
            ->where($where)
            ->select()
            ->toArray();
        //数据总数
        $list['total']=Model::name($this->category)
            ->alias('a')
            ->where($where)
            ->count('a.id');
        return $list;
    }

    /**
     * 产品列表
     * @param array $trade 
     * @param array $where 
     */ 
    public function trade($where=null, $page=1, $page_size=10, $field='*'){
        //矿机列表
        $list['data']= Model::name($this->product)
            ->alias('a')
            ->field($field)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('a.status asc,a.orderid desc')
            ->where($where)
            ->order('orderid desc')
            ->select()
            ->toArray();
        //数据总数
        $list['total']=Model::name($this->product)
            ->alias('a')
            ->where($where)
            ->count('a.id');
        return $list;
    }

    /**
     * 产品信息
     * @param array $tradeinfo 
     * @param array $where 
     */ 
    public function tradeinfo($where=null,$field='*'){
        //列表
        $info = Model::name($this->product)
            ->field($field)
            ->where($where)
            ->find();
        return $info;
    }

    /**
     * 获取K线
     * @param array $getKdata 
     * @param array $where 
     */ 
    public function getKdata($procode=null){
        //列表
        $info = Redis::exec('get',[$procode]);
        return $info;
    }

    /**
     * 操作redis
     * @param array $execRedis
     * @param array $where $page $num
     */
    public function execRedis($meth,$arr,$db='0')
    {
        if($meth){
            $arr[0]=$this->hashfix.$arr[0];
            $data = Redis::exec($meth,$arr,$db);
            return $data;
        }        
        return false;
    }


    /**
     * 设置K线
     * @param array $setKdata 
     * @param array $where 
     */ 
    public function setKdata($procode=null,$data=null,$time=60){
        //列表
        Redis::exec('setex',[$procode,$time,$data]);
        return true;
    }

    /**
     * 订单列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list($where=null, $page=1, $page_size=10, $field='*'){
        //矿机列表
        $list['data']= Model::name($this->order)
            ->field($field)
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('id desc')
            ->select()
            ->toArray();
        //数据总数
        $list['total']=Model::name($this->order)
            ->where($where)
            ->count('id');
        return $list;
    }

    /**
     * 獲取产品实时价格
     * @param array $getprice 
     * @param array $where 
    */ 
    public function getprice($procode=null){ 
        $nowprice=[];
        $key = config('futures.trade');
        switch (true) {
            case !empty($procode):
                $info = Redis::exec('hget',[$this->hashfix.$key,$procode]);
                $info = isset($info)&&!empty($info)?json_decode($info,true):[];
                return $info;
                break;
            default:
                return [];
                break;
        }
    }

    /**
     * 获取订单信息
     * @param array $getorderinfo
     * @param array $data
     */
    public function getorderinfo($where='',$field='*')
    {
        $data=Model::name($this->order)
            ->where($where)
            ->field($field)
            ->find();
        return $data;        
    }
    
    /**
     * 下单
     * @param array $addorder
     * @param array $id
     */
    public function addorder($data)
    {
        $id=Model::name($this->order)->insertGetId($data);
        return $id;        
    }

    /**
     * 结单
     * @param array $updateorder
     * @param array $id
     */
    public function updateorder($where='',$data=[])
    {
        Model::name($this->order)->where($where)->update($data);
        return true;        
    }

    /**
     * 获取订单记录信息
     * @param array $getorderloglist
     * @param array $data
     */
    public function getorderloglist($where='',$field='*')
    {
        $data=Model::name($this->orderlog)
            ->where($where)
            ->field($field)
            ->order('id asc')
            ->select()
            ->toArray();
        foreach ($data as $key => $value) {
            $data[$key]['point']=!empty($value['point'])?json_decode($value['point'],true):[];
        }
        return $data;        
    }

    /**
     * 获取订单记录信息
     * @param array $orderloglist
     * @param array $data
     */
    public function orderloglist($where=null, $page=1, $page_size=10, $field='a.*')
    {
        //列表
        $list['data']= Model::name($this->orderlog)
            ->alias('a')
            ->join($this->order.' b','a.orderid=b.id','left')
            ->field($field.', b.proname as proname, b.titles, b.type as buytype, b.price, b.bond, b.waitbuy, b.wpoint, b.rands, b.procode, b.updown, b.retention, b.lc_fee')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('a.id desc')
            ->select()
            ->toArray();
        foreach ($list['data'] as $key => $value) {
            $list['data'][$key]['point']=!empty($value['point'])?json_decode($value['point'],true):[];
        }
        //数据总数
        $list['total']=Model::name($this->orderlog)
            ->alias('a')
            ->where($where)
            ->count('a.id');
        return $list;       
    }

    /**
     * 获取订单记录信息
     * @param array $getorderloglist
     * @param array $data
     */
    public function getorderloginfo($where='',$field='*')
    {
        $data=Model::name($this->orderlog)
            ->where($where)
            ->field($field)
            ->find();
        $data['point']=!empty($data['point'])?json_decode($data['point'],true):[];
        return $data;        
    }

    /**
     * 下单
     * @param array $addorderlog
     * @param array $id
     */
    public function addorderlog($data)
    {
        $id=Model::name($this->orderlog)->insertGetId($data);
        return $id;        
    }

    /**
     * 下单
     * @param array $updateorderlog
     * @param array $id
     */
    public function updateorderlog($where='',$data=[])
    {
        Model::name($this->orderlog)->where($where)->update($data);
        return true;        
    }
    
    /**
     * 获取数据
     * @param array $getApi 
     * @param array $where 
     */
    public function getApi($query){
        
        $host = $this->config['host'];
        $path = $this->config['getkdata']['path'];
        $method = $this->config['getkdata']['method'];
        $appid = $this->config['appid'];
        $appsecret = $this->config['appsecret'];
        
        $headers = array();
        array_push($headers, "appid: " . $appid);
        array_push($headers, "appsecret: " . $appsecret);
        $url = $host . $path . "?" . $query;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        return curl_exec($curl);
    }

}